import { defineMock } from "../base";

// 全局变量，按cpu编号存储定值数据
const mockDataMap: Record<string, any[]> = {};

// 默认定值数据
const defaultData = () => [
  {
    ID: 1,
    Name: "差动保护启动电流定值",
    Value: 7.0,
    "Current Value": "7.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 2,
    Name: "CT断线告警定值",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 3,
    Name: "CT断线闭锁定值",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 4,
    Name: "母联分段失灵电流定值",
    Value: 2.0,
    "Current Value": "2.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 5,
    Name: "母联分段失灵时间",
    Value: 0.20000000298023224,
    "Current Value": "0.200 s",
    "Lower Limit": 0.0,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 6,
    Name: "低电压闭锁定值",
    Value: 5.0,
    "Current Value": "5.000 V",
    "Lower Limit": 0.0,
    "Upper Limit": 57.70000076293945,
    Step: 0.009999999776482582,
  },
  {
    ID: 7,
    Name: "零序电压闭锁定值",
    Value: 6.0,
    "Current Value": "6.000 V",
    "Lower Limit": 0.0,
    "Upper Limit": 57.70000076293945,
    Step: 0.009999999776482582,
  },
  {
    ID: 8,
    Name: "负序电压闭锁定值",
    Value: 4.0,
    "Current Value": "4.000 V",
    "Lower Limit": 0.0,
    "Upper Limit": 57.70000076293945,
    Step: 0.009999999776482582,
  },
  {
    ID: 9,
    Name: "三相失灵相电流定值",
    Value: 1.0,
    "Current Value": "1.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 10,
    Name: "失灵零序电流定值",
    Value: 1.0,
    "Current Value": "1.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 11,
    Name: "失灵负序电流定值",
    Value: 1.0,
    "Current Value": "1.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 12,
    Name: "失灵保护1时限",
    Value: 0.20000000298023224,
    "Current Value": "0.200 s",
    "Lower Limit": 0.0,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 13,
    Name: "失灵保护2时限",
    Value: 0.5,
    "Current Value": "0.500 s",
    "Lower Limit": 0.0,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 14,
    Name: "充电过流Ⅰ段电流定值",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 15,
    Name: "充电过流Ⅰ段时间",
    Value: 10.0,
    "Current Value": "10.000 s",
    "Lower Limit": 0.009999999776482582,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 16,
    Name: "充电过流Ⅱ段电流定值",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 17,
    Name: "充电零序过流电流定值",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 18,
    Name: "充电过流Ⅱ段时间",
    Value: 10.0,
    "Current Value": "10.000 s",
    "Lower Limit": 0.009999999776482582,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 19,
    Name: "非全相零序电流",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 20,
    Name: "非全相负序电流",
    Value: 20.0,
    "Current Value": "20.000 A",
    "Lower Limit": 0.03999999910593033,
    "Upper Limit": 100.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 21,
    Name: "非全相保护时间",
    Value: 10.0,
    "Current Value": "10.000 s",
    "Lower Limit": 0.10000000149011612,
    "Upper Limit": 10.0,
    Step: 0.009999999776482582,
  },
  {
    ID: 22,
    Name: "差动保护",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
  {
    ID: 23,
    Name: "失灵保护",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
  {
    ID: 24,
    Name: "母联分段充电过流Ⅰ段",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
  {
    ID: 25,
    Name: "母联分段充电过流Ⅱ段",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
  {
    ID: 26,
    Name: "母联分段充电零序过流",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
  {
    ID: 27,
    Name: "母联分段非全相保护",
    Value: 0,
    "Current Value": "0 ",
    "Lower Limit": 0.0,
    "Upper Limit": 1.0,
    Step: 1.0,
  },
];

export default defineMock([
  // GET /api/setting/{cpu}/prot
  {
    url: "setting/:cpu/prot",
    method: ["GET"],
    body({ params }) {
      const { cpu } = params;
      if (!mockDataMap[cpu]) {
        // 初始化数据
        mockDataMap[cpu] = defaultData().map((item) => ({ ...item }));
      }
      return mockDataMap[cpu];
    },
  },
  // GET /api/setting/{cpu}/prot/:zone
  {
    url: "setting/:cpu/prot/:zone",
    method: ["GET"],
    body({ params }) {
      const { cpu } = params;
      if (!mockDataMap[cpu]) {
        mockDataMap[cpu] = defaultData().map((item) => ({ ...item }));
      }
      // 这里只返回前两项，模拟原有行为
      return mockDataMap[cpu].slice(0, 2);
    },
  },
  // POST /api/setting/{cpu}/prot/:zone
  {
    url: "setting/:cpu/prot/:zone",
    method: ["POST"],
    body({ params, body }) {
      const { cpu } = params;
      if (!mockDataMap[cpu]) {
        mockDataMap[cpu] = defaultData().map((item) => ({ ...item }));
      }
      const { settings } = body || {};
      if (Array.isArray(settings)) {
        for (const s of settings) {
          const item = mockDataMap[cpu].find((i) => i.ID === s.id);
          if (item) {
            item.Value = Number(s.Value);
            // 同步更新Current Value，带单位
            if (typeof item["Current Value"] === "string") {
              const unit = item["Current Value"].replace(/^[\d.\-]+\s*/, "");
              item["Current Value"] = `${item.Value.toFixed(3)}${unit ? " " + unit : ""}`;
            }
          }
        }
      }
      return { success: true, message: "success" };
    },
  },
]);
